package com.cmdi.mq.demo1;

import io.netty.channel.DefaultChannelId;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.common.RemotingHelper;

/**
 * @Description: 同步发送消息
 * @Author: gaozb
 */
public class SyncProducer {
    public static void main(String[] args) throws Exception {
        //指定生产者组
        DefaultMQProducer producer = new DefaultMQProducer("producer-group");
        // 设置NameServer的地址
        producer.setNamesrvAddr("10.254.243.246:9876;10.254.243.249:9876");
        //解决invokeSync call timeout异常
        DefaultChannelId.newInstance();
        // 启动Producer实例
        producer.start();
        for (int i = 0; i < 10; i++) {
            //向topic1发送消息。如果没有topic1需要首先创建
            Message message = new Message("topic1", "tagA", ("HelloRocketMQ" + i).getBytes(RemotingHelper.DEFAULT_CHARSET));
            System.out.println(message);
            //同步发送消息，等待发送结果
            SendResult sendResult = producer.send(message);
            System.out.println(sendResult);
        }
        //消息发送完成后，关闭producer服务
        producer.shutdown();
    }
}